package com.yhans.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.yhans.constant.MessageConstant;
import com.yhans.constant.RedisConstant;
import com.yhans.entity.PageResult;
import com.yhans.entity.QueryPageBean;
import com.yhans.entity.Result;
import com.yhans.pojo.CheckGroup;
import com.yhans.pojo.Setmeal;
import com.yhans.service.SetmealService;
import com.yhans.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.nio.channels.MulticastChannel;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;

    //图片上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) throws IOException {

        System.out.println(imgFile);

        //获取文件原始文件名
        //imgFile.getOriginalFilename();
        //文件拓展名
        String extention = imgFile.getOriginalFilename().substring(imgFile.getOriginalFilename().lastIndexOf("."));
        //生成不会重复的文件名
        String fileName = UUID.randomUUID().toString()+extention;

        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);

            //将上传图片名称存入Redis，基于Redis的Set集合存储
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
        } catch (IOException e) {
            e.printStackTrace();

            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }

        return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
    }

    //添加套餐
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){

        try {
            setmealService.add(setmeal,checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();

            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    //套餐分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        PageResult pageQuery = setmealService.pageQuery(queryPageBean);
        return pageQuery;
    }

    //编辑回显套餐
    @RequestMapping("/findById")
    public Result findById(Integer id){

        try {
            Setmeal setmeal = setmealService.findById(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }


    }

    //查询与套餐关联的检查组
    @RequestMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(Integer id){

        try {
            ArrayList<Integer> checkgroupIdsList = setmealService.findCheckGroupIdsBySetmealId(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkgroupIdsList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }

    //编辑修改套餐和关联的检查组信息信息
    @RequestMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {

        try {
            setmealService.edit(setmeal,checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);

    }
    //删除套餐信息
    @RequestMapping("/delete")
    public Result delete(Integer id){

        try {
            setmealService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
    }


}
